package com.bm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bm.Do.Fitment.DetailFitmentDo;
import com.bm.Result.CodeEnum;
import com.bm.Vo.Fitment.DetailFitmentVo;
import com.bm.Vo.Fitment.FitmentListVo;
import com.bm.constant.Constants;
import com.bm.exception.BmException;
import com.bm.mapper.EjyyBuildingInfoMapper;
import com.bm.mapper.EjyyPropertyCompanyUserMapper;
import com.bm.model.EjyyFitment;
import com.bm.model.EjyyPropertyCompanyUser;
import com.bm.query.Fitment.FitmentAgreeQuery;
import com.bm.query.Fitment.FitmentListQuery;
import com.bm.service.EjyyFitmentService;
import com.bm.mapper.EjyyFitmentMapper;
import com.bm.util.DateUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 *
 */
@Service
@Slf4j
public class EjyyFitmentServiceImpl extends ServiceImpl<EjyyFitmentMapper, EjyyFitment>
        implements EjyyFitmentService {

    @Autowired
    private EjyyFitmentMapper ejyyFitmentMapper;

    @Autowired
    private EjyyPropertyCompanyUserMapper ejyyPropertyCompanyUserMapper;

    @Autowired
    private EjyyBuildingInfoMapper ejyyBuildingInfoMapper;

    /**
     * 查询装修登记
     *
     * @param fitmentListQuery
     * @return
     */
    @Override
    public List<FitmentListVo> FitmentList(FitmentListQuery fitmentListQuery) {
        PageHelper.startPage(fitmentListQuery.getPageNum(), fitmentListQuery.getPageSize());
//        LambdaQueryWrapper<EjyyFitment> fitmentLqw = new LambdaQueryWrapper<>();
//        int deposit = fitmentListQuery.getIsReturnCashDeposit();
//        //如果step大于0的话才构建条件
//        fitmentLqw.eq(fitmentListQuery.getStep() > 0,EjyyFitment::getStep,fitmentListQuery.getStep());
//        //如果deposit == 1 或者 == 0 才构建该条件
//        fitmentLqw.eq(deposit == 0 || deposit == 1 , EjyyFitment::getIsReturnCashDeposit,deposit);
        List<FitmentListVo> fitmentListVos = ejyyFitmentMapper.FitemntList(fitmentListQuery);
        return fitmentListVos;
    }


    /**
     * 新增业主报备装修信息
     *
     * @param ejyyFitment
     * @return
     */
    @Override
    @Transactional
    public boolean insertFitment(EjyyFitment ejyyFitment) {
        if (ObjectUtils.isEmpty(ejyyFitment)) {
            throw new BmException(CodeEnum.SYSTEM_PARAMETER_IS_EMPTY);
        }
        //构建条件
        LambdaQueryWrapper<EjyyFitment> lqw = new LambdaQueryWrapper<>();
        //如果社区id不为空的话
        lqw.eq(ejyyFitment.getCommunityId() > 0,
                EjyyFitment::getCommunityId, ejyyFitment.getCommunityId());
        //如果buildingid不为空的话
        lqw.eq(ejyyFitment.getBuildingId() != null,
                EjyyFitment::getBuildingId, ejyyFitment.getBuildingId());
        lqw.eq(ejyyFitment.getWechatMpUserId() != null,
                EjyyFitment::getWechatMpUserId, ejyyFitment.getWechatMpUserId());
        List<EjyyFitment> FitmentsList = ejyyFitmentMapper.selectList(lqw);
        //该业主已提交
        if(FitmentsList.size() > 0){
            return false;
        }
        //封装数据
        ejyyFitment.setCreatedAt(DateUtils.dateConversion());
        ejyyFitment.setStep(Constants.FITMENT_STEP);

        return ejyyFitmentMapper.insert(ejyyFitment) > 0;
    }

    /**
     * 同意装修申请
     * @param fitmentAgreeQuery
     * @return
     */
    @Override
    public boolean updateFitmentAgree(FitmentAgreeQuery fitmentAgreeQuery) {
//        int count = ejyyFitmentMapper.updateFitmentByAgree(fitmentAgreeQuery);
        EjyyFitment ejyyFitment = new EjyyFitment();
        ejyyFitment.setStep(Constants.PROPERTY_COMPANY_ALLOW_STEP);
        //复制对象
        BeanUtils.copyProperties(fitmentAgreeQuery,ejyyFitment);
        //放入同意申请时间
        ejyyFitment.setAgreedAt(DateUtils.dateConversion());
        //bug 业务逻辑混乱 如何获取用户同意的id 这边就先写1  1 默认为管理员
        ejyyFitment.setAgreeUserId(1L);
        int count = ejyyFitmentMapper.updateById(ejyyFitment);
        return count > 0;
    }

    /**
     * 获取用户装修详情信息
     *
     * @param ejyyFitment
     * @return
     */
    @Override
    public DetailFitmentVo detailFitment(EjyyFitment ejyyFitment) {
        DetailFitmentDo fitmentDo = ejyyFitmentMapper.getFitment(ejyyFitment);
//        log.info("{}",fitmentDo);
        if(ObjectUtils.isEmpty(fitmentDo)){
            return null;
        }
        //同意用户信息
        EjyyPropertyCompanyUser AgreeUserInfo = null;
        //如果物业公司同意
        if(fitmentDo.getStep() >= Constants.PROPERTY_COMPANY_ALLOW_STEP){
            //根据同意用户的id查询
            AgreeUserInfo = ejyyPropertyCompanyUserMapper.selectById(fitmentDo.getAgreeUserId());
        }
        //确认用户信息
        EjyyPropertyCompanyUser ConfirmUser = null;
        if(fitmentDo.getStep() == Constants.PROPERTY_COMPANY_CONFIRM_STEP){
            //查询确认用户的id
            ConfirmUser = ejyyPropertyCompanyUserMapper.selectById(fitmentDo.getConfirmUserId());
        }


        //如果返回的操作用户id不为空的话
        EjyyPropertyCompanyUser OperateUser = null;
        if(fitmentDo.getReturnOperateUserId() != null){
            //根据操作用户的id
            OperateUser =  ejyyPropertyCompanyUserMapper.selectById(fitmentDo.getReturnOperateUserId());
        }

        //将数据置为空
        fitmentDo.setAgreeUserId(null);
        fitmentDo.setConfirmUserId(null);
        fitmentDo.setReturnOperateUserId(null);

        //创建返回对象
        DetailFitmentVo detailFitmentVo = new DetailFitmentVo();
        //封装数据
        detailFitmentVo.setDetailFitmentDo(fitmentDo);
        detailFitmentVo.setAgreeUserInfo(AgreeUserInfo);
        detailFitmentVo.setConfirmUserInfo(ConfirmUser);
        detailFitmentVo.setReutrnUserInfo(OperateUser);
        

        return detailFitmentVo;
    }


    /**
     * 根据用户id修改数据
     * @param ejyyFitment
     * @return
     */
    @Override
    public boolean fitmentConfirm(EjyyFitment ejyyFitment) {
        //封装数据
        ejyyFitment.setConfirmedAt(DateUtils.dateConversion());
        ejyyFitment.setStep(Constants.PROPERTY_COMPANY_CONFIRM_STEP);
        ejyyFitment.setConfirmUserId(Constants.ADMIN_ID);
        //创建条件对象
        LambdaQueryWrapper<EjyyFitment> lqw = new LambdaQueryWrapper<>();
        //构建条件
        lqw.eq(EjyyFitment::getCommunityId,ejyyFitment.getCommunityId());
        lqw.eq(EjyyFitment::getId,ejyyFitment.getId());
        lqw.eq(EjyyFitment::getStep,Constants.USER_FINISH_FITMENT_STEP);
        //调用mapper修改数据
//        int count = ejyyFitmentMapper.updateById(ejyyFitment);
        int count = ejyyFitmentMapper.update(ejyyFitment,lqw);

        return count > 0;
    }

    /**
     * 确认装修保证金是否退还
     * @param ejyyFitment
     * @return
     */
    @Override
    public boolean fitmentReturn(EjyyFitment ejyyFitment) {
        //数据
        ejyyFitment.setIsReturnCashDeposit(true);
        ejyyFitment.setReturnOperateUserId(Constants.ADMIN_ID);
        ejyyFitment.setReturnedAt(DateUtils.dateConversion());
        //创建条件对象
        LambdaQueryWrapper<EjyyFitment> lqw = new LambdaQueryWrapper<>();
        //构建条件
        lqw.eq(EjyyFitment::getCommunityId,ejyyFitment.getCommunityId());
        lqw.eq(EjyyFitment::getId,ejyyFitment.getId());
        lqw.eq(EjyyFitment::getStep,Constants.PROPERTY_COMPANY_CONFIRM_STEP);
        //调用查询
//        int count = ejyyFitmentMapper.updateById(ejyyFitment);
        int count = ejyyFitmentMapper.update(ejyyFitment,lqw);

        return count > 0;
    }
}
